package cloud.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cloud.beans.Anime;
import cloud.beans.Person;
import cloud.beans.Review;
import cloud.beans.Staff;
import cloud.forms.AnimeForm;
import cloud.forms.PersonForm;
import cloud.forms.ReviewForm;
import cloud.forms.StaffForm;

public class AnimeSheet extends HttpServlet {

	/**
	 * la page à afficher.
	 */
	private static String VUE = "/WEB-INF/view/animeSheet.jsp";
	
	/**
	 * Permet à la VUE d'avoir accès à l'ensemble des caractéristiques de l'objet Utilisateur
	 */
	public static final String ATT_ANIME = "anime";
	public static final String ATT_STAFF = "staff";
	public static final String ATT_PEOPLE = "people";
	public static final String ATT_REVIEWS = "reviews";
	
	/**
	 * Permet à la VUE d'avoir accès à l'ensemble des caractéristiques de l'objet InscriptionForm
	 */
	public static final String ATT_ANIMEFORM = "animeForm";
	public static final String ATT_STAFFFORM = "staffForm";
	public static final String ATT_REVIEWFORM = "reviewForm";
	
	
	/**********************************************************************************
	*************************************METHODES**************************************


	/**
	 * Affichage de la page.
	 */
	public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
		
		/* Preparation de l'objet formulaire */ 
		AnimeForm animeForm = new AnimeForm();
		StaffForm staffForm = new StaffForm();
		ReviewForm reviewForm = new ReviewForm();
		PersonForm personForm = new PersonForm();
		
		/* Appel au traitement et a la validation de la requete, et recuperation du bean en resultant */
		Anime anime = animeForm.getAnimeById( Long.parseLong(request.getParameter("id")) );
		List<Staff> staffList = staffForm.getList( Long.parseLong(request.getParameter("id")) );
		List<Person> people = new ArrayList<Person>();
		for(Staff staff : staffList) {
			people.add(personForm.getPersonById(staff.getPerson_id()));
		}
		List<Review> reviews = reviewForm.getList( Long.parseLong(request.getParameter("id")) );
		
		request.setAttribute(ATT_ANIME, anime);
		request.setAttribute(ATT_STAFF, staffList);
		request.setAttribute(ATT_PEOPLE, people);
		request.setAttribute(ATT_REVIEWS, reviews);
		
		/* Affichage de la page d'inscription */
		this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
	}

	
	/**
	 * Traitement de la page.
	 */
	public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
		
		/* Affichage de la page d'inscription */
		this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
	}

}